import { AnswerDetailsVo } from '@/services/quest';
import { ExamItem } from '@/store/design/types';
import { Bank, Check } from '../types';

export default class AnsweritemAdapter {
  parse(value: ExamItem): AnswerDetailsVo {
    const answer: AnswerDetailsVo = {
      typeId: String(value.typeId),
      detailsId: value.detailsId ?? '',
      displayOrder: value.questNum - 1,
    };

    // console.log('item', value);

    if (value.answerChose !== undefined) {
      // answer.options = [];
      // 单选 与下拉选择

      if (value.typeId === 2 || value.typeId === 4) {
        answer.options = [];
        if (value.options) {
          // 筛选选中项
          const result = value.options.find(
            (item) => item.displayOrder === Number(value.answerChose as string),
          );
          // console.log(result);
          if (result) {
            // 生产
            const item: Check = {
              answer: String(result.displayOrder),
              answerContent: result.content,
            };
            answer.options.push(item);
          }
        }
      }

      // 多选
      if (value.typeId === 3) {
        console.log('3', value.answerChose);
        answer.options = [];
        (value.answerChose as unknown as string[]).forEach((queNum) => {
          if (value.options) {
            const result = value.options.find(
              (item) => item.displayOrder === Number(queNum),
            );

            if (result) {
              // 生产
              const item: Check = {
                answer: String(result.displayOrder),
                answerContent: result.content,
              };
              answer.options?.push(item);
            }
          }
        });
      }

      // 单项填空
      if (value.typeId === 6) {
        const item: Bank = {
          answer: value.answerChose as string,
        };
        answer.blanks = item;
      }
    }

    return answer;
  }
}
